package com.hs.jhzd.config;

import java.util.Properties;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
public class DbConfig {

  @Autowired
  private MysqlDatabaseProperties databaseProperties;

  @Autowired
  private JpaProperties jpaProperties;

  /**
   * mysql data source.
   * 
   * @return data source bean
   */
  @Bean(name = "primaryDataSource", destroyMethod = "")
  @Primary
  public DataSource primaryDataSource() {
    DataSource dataSource =
        DataSourceBuilder.create().type(org.apache.commons.dbcp2.BasicDataSource.class)
            .driverClassName(databaseProperties.getDriverClassName())
            .username(databaseProperties.getUsername()).password(databaseProperties.getPassword())
            .url(databaseProperties.getUrl()).build();
    return dataSource;
  }

  /**
   * mysql entity manager factory.
   * 
   * @return entity manager factory bean
   */
  @Bean(name = "entityManagerFactory")
  @Primary
  public EntityManagerFactory entityManagerFactory() {
    HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    vendorAdapter.setGenerateDdl(true);
    LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
    factory.setJpaVendorAdapter(vendorAdapter);
    factory.setPackagesToScan("com.hs.jhzd.entity");
    factory.setDataSource(primaryDataSource());
    factory.setJpaProperties(hibernateProperties());
    factory.afterPropertiesSet();

    return factory.getObject();
  }

  /**
   * transaction manager.
   * 
   * @return transaction manager
   */
  @Bean
  @Primary
  public PlatformTransactionManager transactionManager() {
    JpaTransactionManager txManager = new JpaTransactionManager();
    txManager.setEntityManagerFactory(entityManagerFactory());
    return txManager;
  }


  /**
   * hibernate Jpa Vendor Adapter.
   * 
   * @return HibernateJpaVendorAdapter
   */
  @Bean
  @Primary
  public HibernateJpaVendorAdapter hibernateJpaVendorAdapter() {
    HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
    adapter.getJpaPropertyMap().put("databasePlatform", "org.hibernate.dialect.MySQL5Dialect");

    return adapter;
  }

  /**
   * hibernate properties.
   * 
   * @return hibernate properties
   */
  public Properties hibernateProperties() {
    return new Properties() {
      private static final long serialVersionUID = 1L;
      {
        setProperty("hibernate.hbm2ddl.auto", jpaProperties.getDdlAuto());
        setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
        setProperty("hibernate.show_sql", jpaProperties.getShowSql());
        setProperty("hibernate.format_sql", jpaProperties.getFormatSql());
        setProperty("hibernate.physical_naming_strategy",
            "org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy");
      }
    };
  }
}
